import 'dart:math';

import 'package:flutter/material.dart';
import 'package:t3_data_service/biz_data_source/map/address/city_pick/model/host_model.dart';
import 'package:uikit/uikit.dart';

import '../../../configs/poi_search_base_config.dart';
import '../../../configs/poi_search_single_point_config.dart';
import '../../model/const.dart';
import 'input_item.dart';

typedef T3KeywordInputTextGetter = String? Function();
typedef AirportCityChangeCallback = void Function(
    HostModel city, InputItem inputItem);

class T3KeywordInputItemWidget extends StatelessWidget {
  bool isReorderButtonHidden;
  bool isDeleteButtonHidden;
  bool isAddMidWayButtonHidden;
  bool isFirstResponder;

  String placeholder;
  String cityName;
  String? dotImageName;

  Color? dotColor;
  FocusNode? focusNode;
  T3KeywordInputTextGetter textGetter;

  POISearchBaseConfig searchConfig;

  VoidCallback onCityTapped;
  VoidCallback? onAddMidWayButtonTapped;
  VoidCallback? onDeleteButtonTapped;
  VoidCallback? onBecomeFirstResponder;
  ValueChanged<String> onTextChanged;

  T3KeywordInputItemWidget({
    Key? key,
    required this.searchConfig,
    required this.textGetter,
    required this.placeholder,
    required this.focusNode,
    required this.cityName,
    required this.dotColor,
    required this.dotImageName,
    this.isReorderButtonHidden = true,
    this.isDeleteButtonHidden = true,
    this.isAddMidWayButtonHidden = true,
    this.isFirstResponder = true,
    required this.onTextChanged,
    required this.onCityTapped,
    this.onAddMidWayButtonTapped,
    this.onDeleteButtonTapped,
    this.onBecomeFirstResponder,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        if (!isDeleteButtonHidden) ...[
          _deleteButton(),
          const SizedBox(
            width: 4,
          ),
        ],
        Flexible(flex: 1, child: _inputBar(context)),
      ],
    );
  }

  Widget _buildDot() {
    if (dotColor != null) {
      return Container(
        margin: const EdgeInsets.fromLTRB(12, 0, 9, 0),
        decoration: BoxDecoration(
            color: dotColor, borderRadius: BorderRadius.circular(3)),
        width: 6,
        height: 6,
      );
    } else {
      return Container(
        margin: const EdgeInsets.fromLTRB(8, 0, 4, 0),
        width: 16,
        height: 16,
        child: T3Image.t3assets(
          T3AssetsNameString(dotImageName!),
          fit: BoxFit.fill,
        ),
      );
    }
  }

  Widget _buildCity() {
    var citySelectable = true;
    var showCityPoint = true;

    if (searchConfig is POISearchSinglePointConfig) {
      var singleConfig = searchConfig as POISearchSinglePointConfig;
      citySelectable = singleConfig.pageScene != PageScene.feedback;
      showCityPoint = singleConfig.pageScene != PageScene.commonAddress;
    }

    return T3GestureDetector(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          if (showCityPoint) _buildDot(),
          if (!showCityPoint)
            const SizedBox(
              width: 12,
            ),
          Container(
            constraints: const BoxConstraints(
              maxWidth: 45, // 设置最大宽度
            ),
            child: Text(
              cityName,
              style: const TextStyle(fontSize: 14),
              overflow: TextOverflow.ellipsis,
              maxLines: 1,
            ),
          ),
          const SizedBox(
            width: 4,
          ),
          T3Image.t3assets(T3AssetsName.icon_downmore_lucy),
          const SizedBox(
            width: 8,
          ),
          Container(
            width: 1,
            height: 12,
            decoration:
                BoxDecoration(color: const Color(0xFFD2D3D6).withOpacity(0.5)),
          ),
        ],
      ),
      onTap: () {
        if (citySelectable) {
          onCityTapped();
        }
      },
    );
  }

  Widget _buildTextField(BuildContext context) {
    TextEditingController controller = TextEditingController(text: "");
    var text = textGetter() ?? '';
    controller.text = text;
    controller.selection =
        TextSelection.collapsed(offset: controller.text.length);
    return Flexible(
        flex: 1,
        child: Container(
            margin: const EdgeInsets.only(left: 8, right: 4),
            child: TextField(
              style: const TextStyle(fontSize: 14),
              autofocus: isFirstResponder,
              focusNode: focusNode,
              decoration: InputDecoration(
                hintText: text.isEmpty ? placeholder : '',
                border: InputBorder.none,
              ),
              controller: controller,
              textInputAction: TextInputAction.search,
              onChanged: (value) => onTextChanged(value),
              onTap: () {
                if (onBecomeFirstResponder != null) {
                  onBecomeFirstResponder!();
                }
                controller.selection =
                    TextSelection.collapsed(offset: controller.text.length);
              },
              onTapOutside: (event) {
                FocusScope.of(context).requestFocus(FocusNode());
              },
            )));
  }

  Widget _addViaButton() {
    return T3GestureDetector(
      onTap: () {
        if (onAddMidWayButtonTapped != null) {
          onAddMidWayButtonTapped!();
        }
      },
      child: Container(
        width: 40,
        margin: const EdgeInsets.only(right: 8),
        decoration: const BoxDecoration(
            color: Color(0xFFE6FFF9),
            borderRadius: BorderRadius.all(Radius.circular(10))),
        child: Row(
          children: [
            T3Image.t3assets(T3AssetsName.icon_mid_add,
                color: const Color(0xFF00AD59)),
            const Text(
              "途经",
              style: TextStyle(fontSize: 12, color: Color(0xFF00C774)),
            ),
          ],
        ),
      ),
    );
  }

  Widget _reorderButton() {
    return Container(
      width: 40,
      margin: const EdgeInsets.only(right: 8),
      child: T3Image.t3assets(T3AssetsName.icon_taxiflow_sort),
    );
  }

  Widget _deleteButton() {
    return T3GestureDetector(
      onTap: () {
        if (onDeleteButtonTapped != null) {
          onDeleteButtonTapped!();
        }
      },
      child: SizedBox(
        width: 16,
        height: 16,
        child: T3Image.t3assets(T3AssetsName.icon_taxiflow_frontdelect),
      ),
    );
  }

  Widget _inputBar(BuildContext context) {
    return Container(
      margin: EdgeInsets.fromLTRB(
          0,
          0,
          !isDeleteButtonHidden
              ? 0
              : InputConst.kTextFieldRightMarginWhenNoDeleteButton,
          0),
      decoration: BoxDecoration(
          color: const Color(0xFFF7F8FC),
          borderRadius: BorderRadius.circular(InputConst.kInputItemHeight / 2)),
      child: Row(
        children: [
          _buildCity(),
          _buildTextField(context),
          if (!isAddMidWayButtonHidden) _addViaButton(),
          if (!isReorderButtonHidden) _reorderButton(),
        ],
      ),
    );
  }
}
